package com.yyx.app1.My_function.UI;

import android.app.Activity;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.yyx.app1.Data.DataInit;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/10/25.
 */
@EActivity(R.layout.activity_forgetpassword)
public class ForgetPassword_Acitivity extends Activity {
    @ViewById
    EditText phone,newpassword,smscode;

    @Click
    public void home()
    {
        finish();
    }

    @Click
    public void activity_button()
    {
        String phonestr = phone.getText().toString();
        String newpasswordstr = newpassword.getText().toString();
        String smscodestr = smscode.getText().toString();
        if (phonestr.isEmpty() || newpasswordstr.isEmpty() || smscodestr.isEmpty())
            return;
        changePassword(phonestr,newpasswordstr);
    }

    @Click
    public void getcode()
    {
        String str =phone.getText().toString();
        if (str.isEmpty())
            return;
        getSmsCode(str);
    }

    public void changePassword(String phonestr , String passwordstr)
    {
        try {
            StringBuffer stringBuffer = new StringBuffer(DataInit.httpHead);
            stringBuffer.append("updatePwdforgetA").append(".action?mobile=").append(phonestr).append("&password=").append(passwordstr);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(stringBuffer.toString(), null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                Log.d("正常", response.toString());
                            }
                            catch (Exception e)
                            {
                                Log.e("异常1511041004", e.getMessage());
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("异常1511031003", error.getMessage(), error);
                    Toast.makeText(ForgetPassword_Acitivity.this, "网络异常", Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                public com.android.volley.Request.Priority getPriority() {
                    return Priority.HIGH;
                }
            };
            ((MyApp)getApplication()).getBaseDataManager().getQueue().add(jsonObjectRequest);
        }
        catch (Exception e)
        {
            Log.e("异常1511041002", e.getMessage());
        }
    }

    public void getSmsCode(String phonestr)
    {
        try {
            StringBuffer stringBuffer = new StringBuffer(DataInit.httpHead);
            stringBuffer.append("sendSMSCodeForFP").append(".action?mobile=").append(phonestr);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(stringBuffer.toString(), null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                Log.d("正常", response.toString());
                                String message = response.getString("message");
                                if(message.equals("发送失败"))
                                {
                                    Toast.makeText(ForgetPassword_Acitivity.this, "手机号码异常", Toast.LENGTH_LONG).show();
                                }
                            }
                            catch (Exception e)
                            {
                                Log.e("异常1511040940", e.getMessage());
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("异常1511031928", error.getMessage(), error);
                    Toast.makeText(ForgetPassword_Acitivity.this, "网络异常", Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                public com.android.volley.Request.Priority getPriority() {
                    return Priority.HIGH;
                }
            };
            ((MyApp)getApplication()).getBaseDataManager().getQueue().add(jsonObjectRequest);
        }
        catch (Exception e)
        {
            Log.e("异常1511040840", e.getMessage());
        }
    }
}
